package com.springboot.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.dto.WalletRequest;
import com.springboot.model.bo.VirtualWalletBO;
import com.springboot.model.po.VirtualWalletPO;
import com.springboot.model.vo.VirtualWalletVO;
import com.springboot.model.po.*;
import com.springboot.service.VirtualWalletService;

import util.VerifyUtil;

@RefreshScope	//监听动态刷新
@RestController
@RequestMapping("/virtualWallets")
public class VirtualWalletController {
	@Autowired
	private VirtualWalletService virtualWalletService;

	//根据用户编号查询虚拟钱包
	@GetMapping("/users/{userId}")
	public ResponseEntity getVirtualWalletByUserId(@PathVariable String userId , @RequestParam String token){
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !userId.equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		VirtualWalletBO vbo =  virtualWalletService.getVirtualWalletByUserId(userId);
		if(vbo == null) {
    		return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Not exist!");
    	}else {
    		VirtualWalletVO vvo = new VirtualWalletVO(1);
    		vvo.setBalance(vbo.getBalance());
    		vvo.setUserId(vbo.getUserId());
    		vvo.setWalletId(vbo.getWalletId());
    		return ResponseEntity.ok(vvo);
    	}
	}
	
	@GetMapping("/{walletId}")
	public ResponseEntity getVirtualWallet(@PathVariable Integer walletId , @RequestParam String token){
		String userId_target = VerifyUtil.verify(token);
		VirtualWalletBO virtualWallets = virtualWalletService.getVirtualWallet(walletId);
		if(userId_target == null || virtualWallets == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Not exist!");
		}
		if(!virtualWallets.getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		VirtualWalletVO vvo = new VirtualWalletVO(1);
		vvo.setBalance(virtualWallets.getBalance());
		vvo.setUserId(virtualWallets.getUserId());
		vvo.setWalletId(virtualWallets.getWalletId());
		return ResponseEntity.ok(vvo);
	}
	
	// 查询余额
	@GetMapping("/balance/{walletId}")
	public ResponseEntity getBalance(@PathVariable Integer walletId , @RequestParam String token)  {
		VirtualWalletBO virtualWallets = virtualWalletService.getVirtualWallet(walletId);
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !virtualWallets.getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		BigDecimal result = virtualWalletService.getBalance(walletId);
		if(result == null){
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Wrong walletId!"); 
		}else {
			return ResponseEntity.ok(result);
		}
		
	} 
	
	 // 出账（提现）
	@PutMapping("/withdrawal/{walletId}/{amount}")
	public ResponseEntity debit(@PathVariable Integer walletId, @PathVariable BigDecimal amount , @RequestParam String token) {
		if(amount.compareTo(BigDecimal.ZERO) < 0 ) {
			return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("Amount Illegal!");
		}
		VirtualWalletBO virtualWallets = virtualWalletService.getVirtualWallet(walletId);
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !virtualWallets.getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		Integer result = virtualWalletService.debit(walletId, amount);
		if(result.equals(0) || result == null) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred"); 
		}else {
			return ResponseEntity.ok("ok");
		}
	}
	
	// 入账（充值）
	@PutMapping("/topUp/{walletId}/{amount}")
	public ResponseEntity credit(@PathVariable Integer walletId , @PathVariable BigDecimal amount , @RequestParam String token) {
		VirtualWalletBO virtualWallets = virtualWalletService.getVirtualWallet(walletId);
		String userId_target = VerifyUtil.verify(token);
		if(userId_target == null || !virtualWallets.getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		Integer result = virtualWalletService.credit(walletId, amount);
		if(result.equals(0) || result == null) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred"); 
		}else {
			return ResponseEntity.ok("ok");
		}
	} 
	
	//支付请调用orders的updateOrderstate
//	//转账
//	@PutMapping("/transfer")
//	public ResponseEntity transfer(VirtualWalletTransactionPO transaction, WalletRequest walletRequest ) {
//		int result = virtualWalletService.transfer(transaction.getFromWalletId(), transaction.getToWalletId(), walletRequest.getAmount(), walletRequest.getOrderId());
//		if(result == 0){
//			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An internal error occurred"); 
//		}else {
//			return ResponseEntity.ok("ok");
//		}
//	}

}
